//
//  HNSH_GroupQRCodeViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/18.
//

#import "HNSH_GroupQRCodeViewController.h"

//vc
#import "HZZX_ChatScanViewController.h"         //扫码

@interface HNSH_GroupQRCodeViewController ()
/** */
@property (nonatomic, strong)UIView * bgView;
/** */
@property (nonatomic, strong)UIImageView * imgView;
/** */
@property (nonatomic, strong)UILabel * nameLabel;
/** */
@property (nonatomic, strong)UIImageView * codeImgView;
/** */
@property (nonatomic, strong)UILabel * codeLabel;
/** */
@property (nonatomic, strong)UILabel * desLabel;
/** */
@property (nonatomic, strong)UIView * saveView;
/** */
@property (nonatomic, strong)UIImageView * saveImgView;
/** */
@property (nonatomic, strong)UILabel * saveLabel;
/** */
@property (nonatomic, strong)UIView * scanView;
/** */
@property (nonatomic, strong)UIImageView * scanImgView;
/** */
@property (nonatomic, strong)UILabel * scanLabel;

@end

@implementation HNSH_GroupQRCodeViewController

//MARK: Cycle Methods
- (void)viewDidLoad{
    [super viewDidLoad];
    
    [self showNavTitle:@"群二维码名片" showLeftItem:YES];
    self.navView.backgroundColor = [UIColor clearColor];
    
    [self loadQrcodeInfo];
    
    [self addAllViews];
    [self addAllLayout];
}

- (void)addAllLayout{
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(436);
        make.top.mas_equalTo(kNavBarAndStatusBarHeight + 100);
    }];
    [self.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(60);
        make.top.left.mas_equalTo(30);
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.imgView.mas_centerY);
        make.height.mas_equalTo(20);
        make.left.equalTo(self.imgView.mas_right).offset(12);
    }];
    [self.codeImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.bgView);
        make.width.height.mas_equalTo(270);
        make.top.mas_equalTo(104);
    }];
    [self.codeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    [self.desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(17);
        make.bottom.mas_equalTo(-20);
    }];
//    CGFloat x = (kScreenWidth - 70 - 70 * 2) / 2;
//    [self.saveView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(x);
//        make.top.mas_equalTo(self.bgView.mas_bottom).offset(24);
//        make.width.mas_equalTo(70);
//        make.height.mas_equalTo(70);
//    }];
//    [self.saveImgView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.height.mas_equalTo(48);
//        make.centerX.mas_equalTo(0);
//        make.top.mas_equalTo(0);
//    }];
//    [self.saveLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.mas_equalTo(0);
//        make.top.mas_equalTo(self.saveImgView.mas_bottom).offset(8);
//        make.height.mas_equalTo(17);
//    }];
//    [self.scanView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.mas_equalTo(-x);
//        make.top.width.height.mas_equalTo(self.saveView);
//    }];
//    [self.scanImgView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.height.mas_equalTo(48);
//        make.centerX.mas_equalTo(0);
//        make.top.mas_equalTo(0);
//    }];
//    [self.scanLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.mas_equalTo(0);
//        make.top.mas_equalTo(self.scanImgView.mas_bottom).offset(8);
//        make.height.mas_equalTo(17);
//    }];
}

//MARK: Myself Methods
- (void)addAllViews{
    [self.view addSubview:self.bgView];
    [self.bgView addSubview:self.imgView];
    [self.bgView addSubview:self.nameLabel];
    [self.bgView addSubview:self.codeImgView];
    [self.codeImgView addSubview:self.codeLabel];
    [self.bgView addSubview:self.desLabel];
//    [self.view addSubview:self.saveView];
//    [self.saveView addSubview:self.saveImgView];
//    [self.saveView addSubview:self.saveLabel];
//    [self.view addSubview:self.scanView];
//    [self.scanView addSubview:self.scanImgView];
//    [self.scanView addSubview:self.scanLabel];
}

- (void)loadQrcodeInfo{
    [HNSH_ChatNetServer requestRongCloudGroupQrcodeWithGroupId:self.targetId success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            
            NSInteger verifyStatus = [responseObject.resultset[@"verify_status"] integerValue];
            if (verifyStatus) {
                self.codeImgView.image = [UIImage imageNamed:@"chat_qun_qrcode_unUse_icon"];
                self.desLabel.text = @"该群已开启群验证，只可通过邀请进群";
                self.codeLabel.hidden = NO;
//                self.saveView.hidden = YES;
//                self.scanView.hidden = YES;
            } else {
                NSString * url = [NSString stringWithFormat:@"%@%@", k_RY_QR, responseObject.resultset[@"qrcode"]];
                self.codeImgView.image = [UIImage createNonInterpolatedUIImageFormCIImage:[UIImage creatQRcodeWithUrlstring:url] withSize:244];
                self.desLabel.text = responseObject.resultset[@"describe"];
                self.codeLabel.hidden = YES;
//                self.saveView.hidden = YES;
//                self.scanView.hidden = YES;
            }
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

//MARK: Getter Methods
- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor appWhiteLabelColor];
        _bgView.layer.cornerRadius = 20;
        _bgView.layer.masksToBounds = YES;
    }
    return _bgView;
}

- (UIImageView *)imgView{
    if (!_imgView) {
        _imgView = [UIImageView new];
        _imgView.layer.cornerRadius = 5;
        _imgView.layer.masksToBounds = YES;
        _imgView.image = [UIImage imageNamed:@"chat_icon_myqun"];
    }
    return _imgView;
}

- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [UILabel new];
        _nameLabel.font = [UIFont boldSystemFontOfSize:15];
        _nameLabel.textColor = [UIColor colorWithHexString:@"#3C3C3C"];
        _nameLabel.numberOfLines = 0;
        _nameLabel.text = self.name;
    }
    return _nameLabel;
}

- (UIImageView *)codeImgView{
    if (!_codeImgView) {
        _codeImgView = [UIImageView new];
    }
    return _codeImgView;
}

- (UILabel *)codeLabel{
    if (!_codeLabel) {
        _codeLabel = [UILabel new];
        _codeLabel.font = [UIFont boldSystemFontOfSize:20];
        _codeLabel.textColor = [UIColor colorWithHexString:@"#666666"];
        _codeLabel.numberOfLines = 0;
        _codeLabel.textAlignment = NSTextAlignmentCenter;
        _codeLabel.backgroundColor = RGBA(255, 255, 255, 0.91);
        _codeLabel.text = @"该群已开启进群验证\n只可通过邀请进群";
        _codeLabel.hidden = YES;
    }
    return _codeLabel;
}

- (UILabel *)desLabel{
    if (!_desLabel) {
        _desLabel = [UILabel new];
        _desLabel.textAlignment = NSTextAlignmentCenter;
        _desLabel.font = [UIFont systemFontOfSize:12];
        _desLabel.textColor = [UIColor appGrayLabelColor];
        _desLabel.numberOfLines = 0;
//        _desLabel.text = @"该二维码7天内有效，重新进入将更新";
    }
    return _desLabel;
}

- (UIView *)saveView{
    if (!_saveView) {
        _saveView = [UIView new];
        _saveView.backgroundColor = [UIColor clearColor];
        _saveView.hidden = YES;
        UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(saveAction)];
        [_saveView addGestureRecognizer:ges];
    }
    return _saveView;
}

- (UIImageView *)saveImgView{
    if (!_saveImgView) {
        _saveImgView = [UIImageView new];
        _saveImgView.layer.cornerRadius = 5;
        _saveImgView.layer.masksToBounds = YES;
        _saveImgView.image = [UIImage imageNamed:@"chat_qun_save"];
        _saveImgView.hidden = YES;
    }
    return _saveImgView;
}

- (UILabel *)saveLabel{
    if (!_saveLabel) {
        _saveLabel = [UILabel new];
        _saveLabel.textAlignment = NSTextAlignmentCenter;
        _saveLabel.font = [UIFont systemFontOfSize:12];
        _saveLabel.textColor = [UIColor appBlack333LabelColor];
        _saveLabel.text = @"保存到手机";
        _saveLabel.hidden = YES;
    }
    return _saveLabel;
}

- (UIView *)scanView{
    if (!_scanView) {
        _scanView = [UIView new];
        _scanView.backgroundColor = [UIColor clearColor];
    
        UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scanAction)];
        [_scanView addGestureRecognizer:ges];
    }
    return _scanView;
}

- (UIImageView *)scanImgView{
    if (!_scanImgView) {
        _scanImgView = [UIImageView new];
        _scanImgView.layer.cornerRadius = 5;
        _scanImgView.layer.masksToBounds = YES;
        _scanImgView.image = [UIImage imageNamed:@"chat_qun_sao"];
        _scanImgView.hidden = YES;
    }
    return _scanImgView;
}

- (UILabel *)scanLabel{
    if (!_scanLabel) {
        _scanLabel = [UILabel new];
        _scanLabel.textAlignment = NSTextAlignmentCenter;
        _scanLabel.font = [UIFont systemFontOfSize:12];
        _scanLabel.textColor = [UIColor appBlack333LabelColor];
        _scanLabel.text = @"扫描二维码";
        _scanLabel.hidden = YES;
    }
    return _scanLabel;
}


//MARK: 扫码
- (void)scanAction{
    HZZX_ChatScanViewController * vc = [HZZX_ChatScanViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

//MARK: 保存群二维码到系统相册
- (void)saveAction{
    UIImageWriteToSavedPhotosAlbum([self screenShotAction], self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    NSLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo);
    if (!error) {
        [self.view makeToast:@"保存成功"];
    }else{
        [self.view makeToast:[NSString stringWithFormat:@"%@:%@", @"保存失败", error.localizedDescription]];
    }
}

//MARK: 截取群信息view作为分享图片
- (UIImage *)screenShotAction{
    UIGraphicsBeginImageContext(self.bgView.bounds.size);
    [self.bgView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

@end
